<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
      <el-form-item label="客户姓名" prop="customerName" >
        <el-input placeholder="请输入客户姓名" v-model="state.queryForm.customerName" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone" >
        <el-input placeholder="请输入联系电话" v-model="state.queryForm.phone" />
      </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
            >
            新 增
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'pig_carCustomer_export'"
                class="ml10 mr20" style="float: right;"
            @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border 
        :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
        @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center" />
          <el-table-column prop="customerName" label=" 客户名"  show-overflow-tooltip/>
          <el-table-column prop="phone" label=" 联系电话"  show-overflow-tooltip/>
          <el-table-column prop="identityCard" label=" 身份证号"  show-overflow-tooltip/>
         <el-table-column  prop="gender" label=" 性别" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="gender" :value="scope.row.gender"></dict-tag>
          </template>
        </el-table-column>
          <el-table-column prop="orderCount" label=" 订单数"  show-overflow-tooltip>
            <template #default="scope">
              <span style="color: #000000; cursor: pointer;" v-if="scope.row.orderCount!=0" @click="toOrder(scope.row.customerId)">
                {{ scope.row.orderCount }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template #default="scope">
             <el-button icon="edit-pen" text type="primary"
               @click="formDialogRef.openDialog(scope.row.customerId)">编辑</el-button>
            </template>
          </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />

  </div>
</template>

<script setup lang="ts" name="systemCarCustomer">
import { BasicTableProps, useTable } from "/@/hooks/table";
import { fetchList } from "/@/api/hu/carCustomer";
import { useDict } from '/@/hooks/dict';
import { useRouter } from "vue-router";

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典
const { gender } = useDict('gender')
// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
const multiple = ref(true)
//导入路由
const router = useRouter()

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
	tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  getDataList()
}

// 多选事件
const selectionChangHandle = (objs: { customerId: string }[]) => {
  multiple.value = !objs.length;
};
//路由跳转事件
const toOrder = ( customerId: string ) => {
  console.log(customerId)
  router.push({
    name: 'OrderList', // 订单页面的路由 name
    query: {
      customerId: customerId
    }
  })
}



</script>